package com.tstudy.laoshibang.profile;

import java.util.Calendar;
import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.Community;
import com.tstudy.laoshibang.mode.Idxe;
import com.tstudy.laoshibang.mode.MyCommunity;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
/**
 * 微课页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
@SuppressLint("InflateParams")
public class MyCommunityAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<MyCommunity> mItems;

	String todayStr = BaseApplication.getResString(R.string.today);
	String monthStr = BaseApplication.getResString(R.string.month);
	String dayStr = "";
	int conentBg = BaseApplication.getResColor(R.color.common_bg);
	public MyCommunityAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	public int isSameUser;
	
	public void setData(List<MyCommunity> items) {
		setData(items,true);
	}
	public void setData(List<MyCommunity> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<MyCommunity> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.my_community_item, null);
			viewHolder.day = (TextView)convertView.findViewById(R.id.my_community_item_create_time_day);
			viewHolder.month = (TextView)convertView.findViewById(R.id.my_community_item_create_time_month);
			viewHolder.communityLayout = (LinearLayout) convertView.findViewById(R.id.my_community_item_community_layout);
			viewHolder.line = (TextView) convertView.findViewById(R.id.my_community_item_line);
			viewHolder.topLine = (TextView) convertView.findViewById(R.id.my_community_item_top_line);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
//		convertView.setOnClickListener(null);
		return convertView;
	}
	
	int newImageWidth = CommonUtil.dip2px(65);
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final MyCommunity myCommunity = (MyCommunity) getItem(position);
		viewHolder.communityLayout.removeAllViews();
		RelativeLayout.LayoutParams lineLp = (LayoutParams) viewHolder.line.getLayoutParams();
		RelativeLayout.LayoutParams topLineLp = (LayoutParams) viewHolder.topLine.getLayoutParams();
		lineLp.addRule(RelativeLayout.ALIGN_BOTTOM, viewHolder.communityLayout.getId());
		topLineLp.addRule(RelativeLayout.ALIGN_TOP, viewHolder.month.getId());
		Calendar calendar = Calendar.getInstance();
		if (!TextUtils.isEmpty(myCommunity.createTime)) {
			calendar.setTimeInMillis(Long.parseLong(myCommunity.createTime));
		}
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH)+1;
		if (isSameUser == 1) {//我的吐槽显示发布
			if (myCommunity.records == null || myCommunity.records.size() == 0) {
				fillNewIconLayout(viewHolder);
			}else{
				if (CommonUtil.isToday(myCommunity.createTime)) {
					fillNewIconLayout(viewHolder);
				}else{
					viewHolder.day.setText(day+dayStr);
					viewHolder.month.setText(month+monthStr);
				}
				fillItemLayout(viewHolder, myCommunity);
			}
		}else{//其他人吐槽页面
			if (CommonUtil.isToday(myCommunity.createTime)) {
				viewHolder.day.setText(todayStr);
				viewHolder.month.setText("");
			}else{
				viewHolder.day.setText(day+dayStr);
				viewHolder.month.setText(month+monthStr);
			}
			fillItemLayout(viewHolder, myCommunity);
		}
	}
	
	public void fillItemLayout(ViewHolder viewHolder,MyCommunity myCommunity){
		int communitySize = myCommunity.records.size();
		for (int i = 0; i < communitySize;i++) {
			final Community each = myCommunity.records.get(i);
			RelativeLayout itemLayout = (RelativeLayout) mInflater.inflate(R.layout.my_community_record, null);
			RelativeLayout imgLayout = (RelativeLayout) itemLayout.findViewById(R.id.my_community_record_img_layout);
			TextView content = (TextView) itemLayout.findViewById(R.id.my_community_record_content);
			TextView countTxt = (TextView) itemLayout.findViewById(R.id.my_community_record_img_count);
//			TextView deleteTxt = (TextView) itemLayout.findViewById(R.id.my_community_record_delete);
			
			if(each.type.getKey()==1) {
				content.setText("【官方吐槽】  "+each.title);
			}else {
				content.setText(each.content);
			}
//			deleteTxt.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					if (mAapterCallBack != null) {
//						mAapterCallBack.onDeleteClick(each.tcId, position);
//					}
//				}
//			});
			if (each.idxes == null || each.idxes.size()  == 0) {
				countTxt.setVisibility(View.GONE);
				content.setBackgroundColor(conentBg);
				imgLayout.setVisibility(View.GONE);
			}else{
				content.setBackgroundColor(0);
				RelativeLayout.LayoutParams contentLp = (LayoutParams) content.getLayoutParams();
				contentLp.setMargins(0, 0, 0, 0);
				content.setPadding(0, 0, CommonUtil.dip2px(5), 0);;
				imgLayout.setVisibility(View.VISIBLE);
				countTxt.setText("共"+ each.idxes.size()+ "张");
				fillImgLayout(imgLayout, each);
			}
			itemLayout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mAapterCallBack.onViewClick(each.type.getKey(),each.tcId,each.groupId);
				}
			});
			viewHolder.communityLayout.addView(itemLayout);
		}
	}
	
	public void fillNewIconLayout(ViewHolder viewHolder){
		viewHolder.day.setText(todayStr);
		viewHolder.month.setText("");
		ImageView newIcon = new ImageView(BaseApplication.mContext);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(newImageWidth,newImageWidth);
		newIcon.setImageResource(R.drawable.my_community_add);
		newIcon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAapterCallBack != null) {
					mAapterCallBack.onPublishCommunity();
				}
			}
		});
		viewHolder.communityLayout.addView(newIcon,lp);
	}
	public final int MAX_IMG_SIZE = 4;
	public final int mBigImgWidth = (int) BaseApplication.mContext.getResources().getDimension(R.dimen.my_community_record_img_size);
	public final int imageMargin = CommonUtil.dip2px(2);
	public final int mSmallImgWidth = (mBigImgWidth-imageMargin)/2;
	public void fillImgLayout(RelativeLayout layout,Community community){
		int imgSize = Math.min(community.idxes.size(),MAX_IMG_SIZE);
		for (int i = 0; i < imgSize; i++) {
			Idxe each = community.idxes.get(i);
			ImageView imageView = new ImageView(BaseApplication.mContext);
			imageView.setScaleType(ScaleType.FIT_XY);
			RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(mBigImgWidth,mBigImgWidth);
			HttpManager.getInstance().loadCommonImage(imageView, each.imgIdxName+CommonUtil.buildCropUrl(mBigImgWidth, mBigImgWidth));
			imageView.setId(i+1);
			switch (imgSize) {
//			case 1:
//				imgLp.width = mBigImgWidth;
//				imgLp.height = mBigImgWidth;
//				break;
			case 2:
				imgLp.width = mSmallImgWidth;
				imgLp.height = mBigImgWidth;
				if (i==1) {
					imgLp.setMargins(imageMargin, 0, 0, 0);
					imgLp.addRule(RelativeLayout.RIGHT_OF, i);
				}
				break;
			case 3:
				switch (i) {
				case 0:
					imgLp.width = mSmallImgWidth;
					break;
				case 1:
					imgLp.width = mSmallImgWidth;
					imgLp.height = mSmallImgWidth;
					imgLp.setMargins(imageMargin, 0, 0, 0);
					imgLp.addRule(RelativeLayout.RIGHT_OF, i);
					imgLp.addRule(RelativeLayout.ALIGN_TOP, i);
					break;
				case 2:
					imgLp.width = mSmallImgWidth;
					imgLp.height = mSmallImgWidth;
					imgLp.setMargins(imageMargin, imageMargin, 0, 0);
					imgLp.addRule(RelativeLayout.RIGHT_OF, i-1);
					imgLp.addRule(RelativeLayout.BELOW, i);
					break;

				default:
					break;
				}
				break;
			case 4:
				imgLp.width = mSmallImgWidth;
				imgLp.height = mSmallImgWidth;
				switch (i) {
				case 1:
					imgLp.setMargins(0, imageMargin, 0, 0);
					imgLp.addRule(RelativeLayout.BELOW, i);
					break;
				case 2:
					imgLp.setMargins(imageMargin, 0, 0, 0);
					imgLp.addRule(RelativeLayout.RIGHT_OF, i-1);
					break;
				case 3:
					imgLp.setMargins(imageMargin, imageMargin, 0, 0);
					imgLp.addRule(RelativeLayout.RIGHT_OF, i-1);
					imgLp.addRule(RelativeLayout.BELOW, i);
					break;

				default:
					break;
				}
				break;

			default:
				break;
			}
			layout.addView(imageView, imgLp);
		}
	}
	
	static class ViewHolder{
		ImageView userAvatar;
		TextView day;
		TextView month;
		TextView line;
		TextView topLine;
		LinearLayout communityLayout;
	}
	
	public interface AdapterCallBack{
		public void onViewClick(int type,int communityId,String groupId);
		public void onDeleteClick(int communityId,int position);
		public void onPublishCommunity();
	}
	AdapterCallBack mAapterCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mAapterCallBack = actionCallBack;
	}
}
